package com.ruoyi.work.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.work.domain.WorkBillDetail;
import com.ruoyi.work.service.IWorkBillDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工单详情Controller
 *
 * @author xwj
 * @date 2024-08-19
 */
@RestController
@RequestMapping("/system/workBillDetail")
@Api(tags = "工单详情")
public class WorkBillDetailController extends BaseController {
    @Autowired
    private IWorkBillDetailService workBillDetailService;

    /**
     * 查询工单详情列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody WorkBillDetail workBillDetail) {
        startPage();
        List<WorkBillDetail> list = workBillDetailService.selectWorkBillDetailList(workBillDetail);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody WorkBillDetail workBillDetail) {
        startOrderBy();
        List<WorkBillDetail> list = workBillDetailService.selectWorkBillDetailList(workBillDetail);
        return success(list);
    }

    /**
     * 导出工单详情列表
     */
    @Log(title = "工单详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, WorkBillDetail workBillDetail) {
        List<WorkBillDetail> list = workBillDetailService.selectWorkBillDetailList(workBillDetail);
        ExcelUtil<WorkBillDetail> util = new ExcelUtil<WorkBillDetail>(WorkBillDetail.class);
        util.exportExcel(response, list, "工单详情数据");
    }

    /**
     * 获取工单详情详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id) {
        return success(workBillDetailService.selectWorkBillDetailById(id));
    }

    /**
     * 新增工单详情
     */
    @Log(title = "工单详情", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody WorkBillDetail workBillDetail) {
        SysUser user = getLoginUser().getUser();
        if (workBillDetail.getId() == null) {
            workBillDetail.setCreateId(user.getUserId());
            workBillDetail.setCreateTime(DateUtils.getNowDate());
            workBillDetail.setCreateBy(user.getNickName());
            workBillDetailService.insertWorkBillDetail(workBillDetail);
        } else {
            workBillDetail.setUpdateId(user.getUserId());
            workBillDetail.setUpdateTime(DateUtils.getNowDate());
            workBillDetail.setUpdateBy(user.getNickName());
            workBillDetailService.updateWorkBillDetail(workBillDetail);
        }

        return success(workBillDetail);
    }

    /**
     * 修改工单详情
     */
    @Log(title = "工单详情", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody WorkBillDetail workBillDetail) {
        SysUser user = getLoginUser().getUser();
        workBillDetail.setUpdateId(user.getUserId());
        workBillDetail.setUpdateTime(DateUtils.getNowDate());
        workBillDetail.setUpdateBy(user.getNickName());
        return success(workBillDetailService.updateWorkBillDetail(workBillDetail));
    }

    /**
     * 删除工单详情
     */
    @Log(title = "工单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(workBillDetailService.deleteWorkBillDetailByIds(ids));
    }

    @Log(title = "工单详情", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody WorkBillDetail workBillDetail) {
        int num = workBillDetailService.delete(workBillDetail);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<WorkBillDetail>> queryPage(@RequestBody WorkBillDetail workBillDetail) {
        Pages<WorkBillDetail> page = workBillDetailService.queryPage(workBillDetail);
        return ResponseModel.of(page);
    }
}
